home *** CD-ROM | disk | FTP | other *** search
/ PC World 2006 February / PCWorld_2006-02_cd.bin / software / vyzkuste / triky / triky.exe / httrack-3.33.exe / {app} / src / htsalias.c < prev    next >
C/C++ Source or Header  |  2005-02-04  |  18KB  |  542 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsalias.c subroutines:                                */
  34. /*       alias for command-line options and config files        */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htsbase.h"
  42. #include "htsalias.h"
  43. #include "htsglobal.h"
  44.  
  45. void linput(FILE* fp,char* s,int max);
  46. void hts_lowcase(char* s);
  47.  
  48. #define _NOT_NULL(a) ( (a!=NULL) ? (a) : "" )
  49. #define is_realspace(c) (strchr(" \x0d\x0a\x09\x0b\x0c",(c))!=NULL)
  50.  
  51. // COPY OF cmdl_ins in htsmain.c
  52. // Insert a command in the argc/argv
  53. #define cmdl_ins(token,argc,argv,buff,ptr) \
  54.   { \
  55.   int i; \
  56.   for(i=argc;i>0;i--)\
  57.   argv[i]=argv[i-1];\
  58.   } \
  59.   argv[0]=(buff+ptr); \
  60.   strcpybuff(argv[0],token); \
  61.   ptr += (strlen(argv[0])+1); \
  62.   argc++
  63. // END OF COPY OF cmdl_ins in htsmain.c
  64.  
  65.  
  66. /*
  67.   Aliases for command-line and config file definitions
  68.   These definitions can be used:
  69.   in command line:
  70.   --sockets=8       --cache=0
  71.   --sockets 8       --cache off
  72.                     --nocache
  73.   -c8               -C0
  74.   in config file:
  75.   sockets=8         cache=0
  76.   set sockets 8     cache off
  77.  
  78. */
  79. /*
  80.   single : no options
  81.   param  : this option allows a number parameter (1, for example) and can be mixed with other options (R1C1c8)
  82.   param1 : this option must be alone, and needs one distinct parameter (-P <path>)
  83.   param0 : this option must be alone, but the parameter should be put together (+*.gif)
  84. */
  85. const char* hts_optalias[][4] = {
  86.   /*   {"","","",""}, */
  87.   {"path","-O","param1","output path"},
  88.   {"chroot","-%O","param1","default top path"},
  89.   {"mirror","-w","single",""},
  90.   {"mirror-wizard","-W","single",""},
  91.   {"get-files","-g","single",""},
  92.   {"quiet","-q","single",""},
  93.   {"mirrorlinks","-Y","single",""},
  94.   {"proxy","-P","param1","proxy name:port"},
  95.   {"bind","-%b","param1","hostname to bind"},
  96.   {"httpproxy-ftp","-%f","param",""},
  97.   {"depth","-r","param",""},{"recurse-levels","-r","param",""},
  98.   {"ext-depth","-%e","param",""},
  99.   {"max-files","-m","param",""},
  100.   {"max-size","-M","param",""},
  101.   {"max-time","-E","param",""},
  102.   {"max-rate","-A","param",""},
  103.   {"max-pause","-G","param",""},
  104.   {"sockets","-c","param","number of simultaneous connections allowed"},{"socket","-c","param","number of simultaneous connections allowed"},{"connection","-c","param","number of simultaneous connections allowed"},
  105.   {"connection-per-second","-%c","param","number of connection per second allowed"},
  106.   {"timeout","-T","",""},
  107.   {"retries","-R","param","number of retries for non-fatal errors"},
  108.   {"min-rate","-J","param",""},
  109.   {"host-control","-H","param",""},
  110.   {"extended-parsing","-%P","param",""},
  111.   {"near","-n","single",""},
  112.   {"test","-t","single",""},
  113.   {"list","-%L","param1",""},
  114.   {"urllist","-%S","param1",""},
  115.   {"language","-%l","param1",""}, {"lang","-%l","param1",""},
  116.   {"structure","-N","param",""}, {"user-structure","-N","param1",""},
  117.   {"long-names","-L","param",""},
  118.   {"keep-links","-K","param",""},
  119.   {"mime-html","-%M","single",""}, {"mht","-%M","single",""},
  120.   {"replace-external","-x","single",""},
  121.   {"disable-passwords","-%x","single",""},{"disable-password","-%x","single",""},
  122.   {"include-query-string","-%q","single",""},
  123.   {"generate-errors","-o","single",""},
  124.   {"purge-old","-X","param",""},
  125.   {"cookies","-b","param",""},
  126.   {"check-type","-u","param",""},
  127.   {"assume","-%A","param1",""}, {"mimetype","-%A","param1",""},
  128.   {"parse-java","-j","param",""},
  129.   {"protocol","-@i","param",""},
  130.   {"robots","-s","param",""},
  131.   {"http-10","-%h","single",""},{"http-1.0","-%h","single",""},
  132.   {"keep-alive","-%k","single",""},
  133.   {"build-top-index","-%i","single",""},
  134.   {"disable-compression","-%z","single",""},
  135.   {"tolerant","-%B","single",""},
  136.   {"updatehack","-%s","single",""}, {"sizehack","-%s","single",""},
  137.   {"urlhack","-%u","single",""},
  138.   {"user-agent","-F","param1","user-agent identity"},
  139.   {"referer","-%R","param1","default referer URL"},
  140.   {"from","-%E","param1","from email address"},
  141.   {"footer","-%F","param1",""},
  142.   {"cache","-C","param","number of retries for non-fatal errors"},
  143.   {"store-all-in-cache","-k","single",""},
  144.   {"do-not-recatch","-%n","single",""},
  145.   {"do-not-log","-Q","single",""},
  146.   {"extra-log","-z","single",""},
  147.   {"debug-log","-Z","single",""},
  148.   {"verbose","-v","single",""},
  149.   {"file-log","-f","single",""},
  150.   {"single-log","-f2","single",""},
  151.   {"index","-I","single",""},
  152.   {"search-index","-%I","single",""},
  153.   {"priority","-p","param",""},
  154.   {"debug-headers","-%H","single",""},
  155.   {"userdef-cmd","-V","param1",""},
  156.   {"callback","-%W","param1","plug an external callback"}, {"wrapper","-%W","param1","plug an external callback"},
  157.   {"structure","-N","param1","user-defined structure"},
  158.   {"usercommand","-V","param1","user-defined command"},
  159.   {"display","-%v","single","show files transfered and other funny realtime information"},
  160.   {"dos83","-L0","single",""},
  161.   {"iso9660","-L2","single",""},
  162.   /* */
  163.  
  164.   /* DEPRECATED */
  165.   {"stay-on-same-dir","-S","single","stay on the same directory - DEPRECATED"},
  166.   {"can-go-down","-D","single","can only go down into subdirs - DEPRECATED"},
  167.   {"can-go-up","-U","single","can only go to upper directories- DEPRECATED"},
  168.   {"can-go-up-and-down","-B","single","can both go up&down into the directory structure - DEPRECATED"},
  169.   {"stay-on-same-address","-a","single","stay on the same address - DEPRECATED"},
  170.   {"stay-on-same-domain","-d","single","stay on the same principal domain - DEPRECATED"},
  171.   {"stay-on-same-tld","-l","single","stay on the same TLD (eg: .com) - DEPRECATED"},
  172.   {"go-everywhere","-e","single","go everywhere on the web - DEPRECATED"},
  173.  
  174.   /* Badly documented */
  175.   {"debug-testfilters","-#0","param1","debug: test filters"},
  176.   {"advanced-flushlogs","-#f","single",""},
  177.   {"advanced-maxfilters","-#F","param",""},
  178.   {"version","-#h","single",""},
  179.   {"debug-scanstdin","-#K","single",""},
  180.   {"advanced-maxlinks","-#L","single",""},
  181.   {"advanced-progressinfo","-#p","single","deprecated"},
  182.   {"catch-url","-#P","single","catch complex URL through proxy"},
  183.   {"debug-oldftp","-#R","single",""},
  184.   {"debug-xfrstats","-#T","single",""},
  185.   {"advanced-wait","-#u","single",""},
  186.   {"debug-ratestats","-#Z","single",""},
  187.   {"exec","-#!","param1",""},
  188.   {"fast-engine","-#X","single","Enable fast routines"},
  189.   {"debug-overflows","-#X0","single","Attempt to detect buffer overflows"},
  190.   {"debug-cache","-#C","param1","List files in the cache"},
  191.   {"extract-cache","-#C","single","Extract meta-data"},
  192.   {"debug-parsing","-#d","single","debug: test parser"},
  193.   {"repair-cache","-#R","single","repair the damaged cache ZIP file"}, {"repair","-#R","single",""},
  194.  
  195.   /* STANDARD ALIASES */
  196.   {"spider","-p0C0I0t","single",""},
  197.   {"testsite","-p0C0I0t","single",""},
  198.   {"testlinks","-r1p0C0I0t","single",""}, {"test","-r1p0C0I0t","single",""}, {"bookmark","-r1p0C0I0t","single",""},
  199.   {"mirror","-w","single",""},
  200.   {"testscan","-p0C0I0Q","single",""}, {"scan","-p0C0I0Q","single",""}, {"check","-p0C0I0Q","single",""},
  201.   {"skeleton","-p1","single",""},
  202.   {"preserve","-%p","single",""},
  203.   {"get","-qg","single",""},
  204.   {"update","-iC2","single",""},
  205.   {"continue","-iC1","single",""}, {"restart","-iC1","single",""},
  206.   {"continue","-i","single",""}, /* for help alias */
  207.   {"sucker","-r999","single",""},
  208.   {"help","-h","single",""}, {"documentation","-h","single",""}, {"doc","-h","single",""},
  209.   {"wide","-c32","single",""},
  210.   {"tiny","-c1","single",""},
  211.   {"ultrawide","-c48","single",""},
  212.   {"http10","-%h","single",""},
  213.   {"filelist","-%L","single",""}, {"list","-%L","single",""},
  214.   {"filterlist","-%S","single",""},
  215.   /* END OF ALIASES */
  216.  
  217.   /* Filters */
  218.   {"allow","+","param0","allow filter"},
  219.   {"deny","-","param0","deny filter"},
  220.   /* */
  221.  
  222.   /* URLs */
  223.   {"add","","param0","add URLs"},
  224.   /* */
  225.  
  226.   /* Specific */
  227.   {"user","-%U","param1","output path"},
  228.   /* */
  229.  
  230.   /* Internal */
  231.   {"catchurl","--catchurl","single","catch complex URL through proxy"},
  232.   {"updatehttrack","--updatehttrack","single","update HTTrack Website Copier"},
  233.   {"clean","--clean","single","clean up log files and cache"},
  234.   {"tide","--clean","single","clean up log files and cache"},
  235.   {"autotest","-#T","single",""},
  236.   /* */
  237.  
  238.   {"","","",""}
  239. };
  240.  
  241.  
  242. /* 
  243.   Check for alias in command-line 
  244.   argc,argv     as in main()
  245.   n_arg         argument position
  246.   return_argv   a char[2][] where to put result
  247.   return_error  buffer in case of syntax error
  248.  
  249.   return value: number of arguments treated (0 if error)
  250. */
  251. int optalias_check(int argc,const char * const * argv,int n_arg,
  252.                    int* return_argc,char** return_argv,
  253.                    char* return_error) {
  254.   return_error[0]='\0';
  255.   *return_argc=1;
  256.   if (argv[n_arg][0]=='-')
  257.   if (argv[n_arg][1]=='-') {
  258.     char command[1000];
  259.     char param[1000];
  260.     char addcommand[256];
  261.     /* */
  262.     char* position;
  263.     int need_param=1;
  264.     //int return_param=0;
  265.     int pos;
  266.     command[0]=param[0]=addcommand[0]='\0';
  267.  
  268.     /* --sockets=8 */
  269.     if ( (position=strchr(argv[n_arg],'=')) ) {
  270.       /* Copy command */
  271.       strncatbuff(command,argv[n_arg]+2,(int) (position - (argv[n_arg]+2)) );
  272.       /* Copy parameter */
  273.       strcpybuff(param,position+1);
  274.     }
  275.     /* --nocache */
  276.     else if (strncmp(argv[n_arg]+2,"no",2)==0) {
  277.       strcpybuff(command,argv[n_arg]+4);
  278.       strcpybuff(param,"0");
  279.     }
  280.     /* --sockets 8 */
  281.     else {
  282.       if (strncmp(argv[n_arg]+2,"wide-",5)==0) {
  283.         strcpybuff(addcommand,"c32");
  284.         strcpybuff(command,strchr(argv[n_arg]+2,'-')+1);
  285.       } else if (strncmp(argv[n_arg]+2,"tiny-",5)==0) {
  286.         strcpybuff(addcommand,"c1");
  287.         strcpybuff(command,strchr(argv[n_arg]+2,'-')+1);
  288.       } else
  289.         strcpybuff(command,argv[n_arg]+2);
  290.       need_param=2;
  291.     }
  292.  
  293.     /* Now solve the alias */
  294.     pos=optalias_find(command);
  295.     if (pos>=0) {
  296.       /* Copy real name */
  297.       strcpybuff(command,hts_optalias[pos][1]);
  298.       /* With parameters? */
  299.       if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  300.         /* Copy parameters? */
  301.         if (need_param == 2) {
  302.           if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  303.             sprintf(return_error,
  304.               "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  305.               command,command,_NOT_NULL(optalias_help(command)));
  306.             return 0;
  307.           }
  308.           strcpybuff(param,argv[n_arg+1]);
  309.           need_param=2;
  310.         }
  311.       } else
  312.         need_param=1;
  313.  
  314.       /* Final result */
  315.  
  316.       /* Must be alone (-P /tmp) */
  317.       if (strcmp(hts_optalias[pos][2],"param1")==0) {
  318.         strcpybuff(return_argv[0],command);
  319.         strcpybuff(return_argv[1],param);
  320.         *return_argc=2;     /* 2 parameters returned */
  321.       } 
  322.       /* Alone with parameter (+*.gif) */
  323.       else if (strcmp(hts_optalias[pos][2],"param0")==0) {
  324.         /* Command */
  325.         strcpybuff(return_argv[0],command);
  326.         strcatbuff(return_argv[0],param);
  327.       }
  328.       /* Together (-c8) */
  329.       else {
  330.         /* Command */
  331.         strcpybuff(return_argv[0],command);
  332.         /* Parameters accepted */
  333.         if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  334.           /* --cache=off or --index=on */
  335.           if (strcmp(param,"off")==0)
  336.             strcatbuff(return_argv[0],"0");
  337.           else if (strcmp(param,"on")==0) {
  338.             // on is the default
  339.             // strcatbuff(return_argv[0],"1");
  340.           } else
  341.             strcatbuff(return_argv[0],param);
  342.         }
  343.         *return_argc=1;     /* 1 parameter returned */
  344.       }
  345.     } else {
  346.       sprintf(return_error,"Unknown option: %s\n",command);
  347.       return 0;
  348.     }
  349.     return need_param;
  350.   }
  351.  
  352.   /* Check -O <path> */
  353.   {
  354.     int pos;
  355.     if ((pos=optreal_find(argv[n_arg]))>=0) {
  356.       if ( (strcmp(hts_optalias[pos][2],"param1")==0) || (strcmp(hts_optalias[pos][2],"param0")==0)) {
  357.         if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  358.           sprintf(return_error,
  359.             "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  360.             argv[n_arg],argv[n_arg],_NOT_NULL(optalias_help(argv[n_arg])));
  361.           return 0;
  362.         }
  363.         /* Copy parameters */
  364.         strcpybuff(return_argv[0],argv[n_arg]);
  365.         strcpybuff(return_argv[1],argv[n_arg+1]);
  366.         /* And return */
  367.         *return_argc=2;     /* 2 parameters returned */
  368.         return 2;           /* 2 parameters used */
  369.       }
  370.     }
  371.   }
  372.   
  373.   /* Copy and return other unknown option */
  374.   strcpybuff(return_argv[0],argv[n_arg]);
  375.   return 1;
  376. }
  377.  
  378. /* Finds the <token> option alias and returns the index, or -1 if failed */
  379. int optalias_find(const char* token) {
  380.   if (token[0] != '\0') {
  381.     int i=0;
  382.     while(hts_optalias[i][0][0] != '\0') {
  383.       if (strcmp(token,hts_optalias[i][0])==0) {
  384.         return i;
  385.       }
  386.       i++;
  387.     }
  388.   }
  389.   return -1;
  390. }
  391.  
  392. /* Finds the <token> real option and returns the index, or -1 if failed */
  393. int optreal_find(const char* token) {
  394.   if (token[0] != '\0') {
  395.     int i=0;
  396.     while(hts_optalias[i][0][0] != '\0') {
  397.       if (strcmp(token,hts_optalias[i][1])==0) {
  398.         return i;
  399.       }
  400.       i++;
  401.     }
  402.   }
  403.   return -1;
  404. }
  405.  
  406. const char* optreal_value(int p) {
  407.   return hts_optalias[p][1];
  408. }
  409. const char* optalias_value(int p) {
  410.   return hts_optalias[p][0];
  411. }
  412. const char* opttype_value(int p) {
  413.   return hts_optalias[p][2];
  414. }
  415. const char* opthelp_value(int p) {
  416.   return hts_optalias[p][3];
  417. }
  418.  
  419. /* Help for option <token>, empty if not available, or NULL if unknown <token> */
  420. const char* optalias_help(const char* token) {
  421.   int pos=optalias_find(token);
  422.   if (pos>=0)
  423.     return hts_optalias[pos][3];
  424.   else
  425.     return NULL;
  426. }
  427.  
  428. /* Include a file to the current command line */
  429. /* example:
  430.   set sockets 8
  431.   index on
  432.   allow *.gif
  433.   deny ad.*
  434. */
  435. int optinclude_file(const char* name,
  436.                     int* argc,char** argv,char* x_argvblk,int* x_ptr) {
  437.   FILE* fp;
  438.   fp=fopen(name,"rb");
  439.   if (fp) {
  440.     char line[256];
  441.     int insert_after=1;       /* first, insert after program filename */
  442.     while(!feof(fp)) {
  443.       char *a,*b;
  444.       int result;
  445.       
  446.       /* read line */
  447.       linput(fp,line,250);
  448.       hts_lowcase(line);
  449.       if (strnotempty(line)) {
  450.         /* no comment line: # // ; */
  451.         if (strchr("#/;",line[0])==NULL) {
  452.           /* right trim */
  453.           a=line+strlen(line)-1;
  454.           while(is_realspace(*a)) *(a--) = '\0';
  455.           /* jump "set " and spaces */
  456.           a=line;
  457.           while(is_realspace(*a)) a++;
  458.           if (strncmp(a,"set",3)==0) {
  459.             if (is_realspace(*(a+3))) {
  460.               a+=4;
  461.             }
  462.           }
  463.           while(is_realspace(*a)) a++;
  464.           /* delete = ("sockets=8") */
  465.           if ( (b=strchr(a,'=')) )
  466.             *b=' ';
  467.           
  468.           /* isolate option and parameter */
  469.           b=a;
  470.           while( (!is_realspace(*b)) && (*b) ) b++;
  471.           if (*b) {
  472.             *b='\0';
  473.             b++;
  474.           }
  475.           /* a is now the option, b the parameter */
  476.           
  477.           {
  478.             int return_argc;
  479.             char return_error[256];
  480.             char  _tmp_argv[4][HTS_CDLMAXSIZE];
  481.             char*  tmp_argv[4];
  482.             tmp_argv[0]=_tmp_argv[0]; tmp_argv[1]=_tmp_argv[1]; tmp_argv[2]=_tmp_argv[2]; tmp_argv[3]=_tmp_argv[3];
  483.             strcpybuff(tmp_argv[0],"--");
  484.             strcatbuff(tmp_argv[0],a);
  485.             strcpybuff(tmp_argv[1],b);
  486.             
  487.             result=optalias_check(2,(const char * const *)tmp_argv,0,
  488.               &return_argc,(tmp_argv+2),
  489.               return_error);
  490.             if (!result) {
  491.               printf("%s\n",return_error);
  492.             } else {
  493.               int insert_after_argc;
  494.               /* Insert parameters BUT so that they can be in the same order */
  495.               /* temporary argc: Number of parameters after minus insert_after_argc */
  496.               insert_after_argc=(*argc)-insert_after;
  497.               cmdl_ins((tmp_argv[2]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  498.               *argc=insert_after_argc+insert_after;
  499.               insert_after++;
  500.               /* Second one */
  501.               if (return_argc>1) {
  502.                 insert_after_argc=(*argc)-insert_after;
  503.                 cmdl_ins((tmp_argv[3]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  504.                 *argc=insert_after_argc+insert_after;
  505.                 insert_after++;
  506.               }
  507.               /* increment to nbr of used parameters */
  508.               /* insert_after+=result; */
  509.             }
  510.           }
  511.         }
  512.         
  513.       }
  514.     }
  515.     fclose(fp);
  516.     return 1;
  517.   }
  518.   return 0;
  519. }
  520.  
  521. /* Get home directory, '.' if failed */
  522. /* example: /home/smith */
  523. char* hts_gethome(void) {
  524. #ifndef _WIN32_WCE
  525.   char* home = getenv( "HOME" );
  526.   if (home)
  527.     return home;
  528.   else
  529. #endif
  530.     return ".";
  531. }
  532.  
  533. /* Convert ~/foo into /home/smith/foo */
  534. void expand_home(char* str) {
  535.   if (str[0] == '~') {
  536.     char BIGSTK tempo[HTS_URLMAXSIZE*2];
  537.     strcpybuff(tempo,hts_gethome());
  538.     strcatbuff(tempo,str+1);
  539.     strcpybuff(str,tempo);
  540.   }
  541. }
  542.